package com.ikas.ai.server.module.system.controller;

import com.github.pagehelper.PageInfo;
import com.ikas.ai.handle.JsonResult;
import com.ikas.ai.handle.JsonResultType;
import com.ikas.ai.server.aop.ColumnCheck;
import com.ikas.ai.server.module.AbstractBean;
import com.ikas.ai.server.module.system.model.LogOpsType;
import com.ikas.ai.server.module.system.model.Permission;
import com.ikas.ai.server.module.system.model.vo.PermissionVo;
import com.ikas.ai.server.module.system.service.PermissionService;
import com.ikas.ai.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
/**
 * 系统管理-权限管理
 *
 * @author deng.baohui
 * @description 权限模板管理接口
 * @date 2021/12/1
 */
@RestController
@RequestMapping("/system/permission")
public class PermissionController extends AbstractBean {

    private static final String COLUMN_NAMES = "id,name";

    @Autowired
    private PermissionService permissionService;

    /**
     * 权限分页查询
     *
     * @return
     * @ColumnCheck(value = COLUMN_NAMES) String sortColumnName
     */
    @GetMapping("/page")
    public String pageQueryPermissionList(@RequestParam(value = "name", required = false) String name,
                                          @RequestParam(value = "type", required = false) Integer type,
                                          @RequestParam(value = "menuLevel", required = false) Integer level,
                                          @RequestParam(value = "page", required = false) Integer pageNo,
                                          @RequestParam(value = "size", required = false) Integer pageSize,
                                          @RequestParam(value = "sc", required = false) @ColumnCheck(value = COLUMN_NAMES) String sortColumnName,
                                          @RequestParam(value = "so", required = false) String sortOrder) {
        JsonResult jsonResult = new JsonResult(JsonResultType.OK);
        PageUtil.configPageHelper(pageNo, pageSize, sortColumnName, sortOrder, "orderby", "asc");
        List<Permission> list = permissionService.pageQueryPermissionList(name, type, level);
        PageInfo<Permission> pageInfo = new PageInfo<>(list);
        jsonResult.add(pageInfo);
        return jsonResult.toJSON();
    }

    /**
     * 选择菜单(添加、修改菜单)
     */
    @GetMapping("/select")
    public String select() {
        JsonResult jsonResult = new JsonResult(JsonResultType.OK);
        //查询列表数据
        List<Permission> menuList = permissionService.queryNotButtonList();
        //添加顶级菜单
        Permission root = new Permission();
        root.setId(0L);
        root.setCode("0");
        root.setName("顶级菜单");
        root.setPid(-1L);
        root.setOpen(true);
        menuList.add(root);
        jsonResult.add("menuList", menuList);
        return jsonResult.toJSON();
    }

    /**
     * 保存权限
     *
     * @return
     */
    @PostMapping("/save")
    public String save(HttpServletRequest request, @RequestBody @Valid Permission permission, BindingResult bindingResult) {
        JsonResult jsonResult = new JsonResult(JsonResultType.OK);
        permissionService.saveOrUpdatePermission(permission);
        this.logOperation(request, LogOpsType.MENU, String.format("新增菜单信息，用户ID为[%s],菜单ID为[%s]", getUserId(), permission.getId()));
        return jsonResult.toJSON();
    }

    /**
     * 修改权限
     *
     * @return
     */
    @PostMapping("/update")
    public String update(HttpServletRequest request, @RequestBody @Valid Permission permission, BindingResult bindingResult) {
        JsonResult jsonResult = new JsonResult(JsonResultType.OK);
        permissionService.saveOrUpdatePermission(permission);
        this.logOperation(request, LogOpsType.MENU, String.format("更新菜单信息，用户ID为[%s],菜单ID为[%s]", getUserId(), permission.getId()));
        return jsonResult.toJSON();
    }

    /**
     * 通过id查询菜单信息
     *
     * @return
     */
    @GetMapping("/info/{id}")
    public String info(@PathVariable("id") @Valid @NotEmpty(message = "id不能为空") Integer id) {
        JsonResult jsonResult = new JsonResult(JsonResultType.OK);
        Permission permission = permissionService.getById(id);
        jsonResult.add(permission);
        return jsonResult.toJSON();
    }

    /**
     * 删除权限
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public String delete(HttpServletRequest request, @PathVariable("id") @Valid @NotNull(message = "id不能为空") Long id) {
        JsonResult jsonResult = new JsonResult(JsonResultType.OK);
        permissionService.deleteById(id);
        this.logOperation(request, LogOpsType.MENU, String.format("删除权限菜单信息，用户ID为[%s],菜单ID为[%s]", getUserId(), id));
        return jsonResult.toJSON();
    }

    /**
     * 生成按钮权限
     * @param permissionVo 权限信息
     * @return String
     */
    @PostMapping("/generateButton")
    public String generateButton(@RequestBody @Valid PermissionVo permissionVo, BindingResult bindingResult){
        JsonResult jsonResult = new JsonResult(JsonResultType.OK);
        Permission permission = permissionService.getById(permissionVo.getMenuId());
        Assert.notNull(permission,"当前菜单不存在");
        permissionService.generateButton(permissionVo);
        jsonResult.setMessage("生成按钮权限成功");
        return jsonResult.toJSON();
    }

}
